package playground.fs.rnkrsoft;

import java.io.*;
import java.util.concurrent.Callable;

/**
 * Created by test on 2020/3/5.
 * 查找任务
 */
public class FindTask implements Callable<FindTaskResult> {
    /**
     * 读取的任务文件
     */
    File file;
    /**
     * 搜索字符串
     */
    String searchStr;
    /**
     * 字符串查找器
     */
    final StringFinder stringFinder = new StringFinderImpl();

    public FindTask(File file, String searchStr) {
        this.file = file;
        this.searchStr = searchStr;
    }

    /**
     * 任务体
     * @return 任务结果
     * @throws IOException
     */
    FindTaskResult run0() throws IOException {
        FileReader reader = new FileReader(file);
        return stringFinder.find(new BufferedReader(reader), searchStr);
    }

    @Override
    public FindTaskResult call() throws Exception {
        try {
            return run0();
        } catch (IOException e) {
            //nothing
            e.printStackTrace();
            return null;
        }
    }
}